package com.yunyao.common.web.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @author xingjishuai
 * @date 2024-07-10
 */
@Data
public class PageResult<T> implements Iterable<T>, Serializable {
    private static final long serialVersionUID = -3720998571176536865L;

    /**
     * 数据条数
     * -- SETTER --
     * 设置内容
     *
     * @param records 内容
     */
    private List<T> records = new ArrayList<>();

    /**
     * 一共有多少条数据
     * -- SETTER --
     * 设置全部元素数目
     * -- GETTER --
     * 获取全部元素数目
     */
    private long totalElements;

    /**
     * 当前页数
     */
    private long currentPage;

    /**
     * 一页最多有多少条数据
     */
    private long pageSize;

    /**
     * 一共有多少页
     * -- GETTER --
     * 获取总的页数
     *
     * @return long
     */
    private long totalPages;

    /**
     * 当前页数有多少条数据
     * -- SETTER --
     * 设置单页元素数目
     * -- GETTER --
     * 获取单页元素数目
     */
    private long numberOfElements;

    public PageResult() {
    }

    /**
     * @param records
     * @param currentPage
     * @param pageSize
     * @param totalPages
     * @param totalElements
     */
    public PageResult(List<T> records, long currentPage, long pageSize, long totalPages, long totalElements) {
        this.records = records;
        this.totalElements = totalElements;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.numberOfElements = records.size();
        this.totalPages = totalPages;
    }

    /**
     * @param records
     * @param page
     */
    public PageResult(List<T> records, IPage page) {
        this.records = records;
        this.totalElements = page.getTotal();
        this.currentPage = page.getCurrent();
        this.pageSize = page.getSize();
        this.numberOfElements = records.size();
        this.totalPages = page.getPages();
    }

    /**
     * currentPage -1 不分页
     * @param totalList 全部数据 分页
     * @param currentPage  当前页码
     * @param pageSize     每页大小
     */
    public PageResult(List<T> totalList, long currentPage, long pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalElements = totalList.size();
        if (currentPage > 0) {
            int pages = (int) Math.ceil((double) totalElements / pageSize);
            this.totalPages = pages;
            /**
             * 获取当前页的数据
             */
            int fromIndex = (int) ((currentPage - 1) * pageSize);
            int toIndex = (int) Math.min(currentPage * pageSize, totalElements);
            List<T> records = totalList.subList(fromIndex, toIndex);
            this.records = records;
        }else{
            this.records = totalList;
        }
        this.numberOfElements = records.size();
    }

    /**
     * 是否有前一页
     *
     * @return boolean
     */
    public boolean hasPrevious() {
        return getCurrentPage() > 0;
    }

    /**
     * 是否有下一页
     *
     * @return boolean
     */
    public boolean hasNext() {
        return getCurrentPage() + 1 < getTotalPages();
    }


    /**
     * 获取当前页的数据
     *
     * @return List<T>
     */
    public List<T> getRecords() {
        return Collections.unmodifiableList(records);
    }

    /**
     * 是否有内容
     *
     * @return boolean
     */
    public boolean hasRecords() {
        return getNumberOfElements() > 0;
    }

    /**
     * 获取单页大小
     */
    public Long getPageSize() {
        return pageSize;
    }

    /**
     * 设置单页大小
     */
    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }


    /**
     * 获取当前页号
     */
    public Long getCurrentPage() {
        return currentPage;
    }

    /**
     * 设置当前页号
     */
    public void setCurrentPage(Long currentPage) {
        this.currentPage = currentPage;
    }


    /**
     * 迭代器
     */
    @Override
    public Iterator<T> iterator() {
        return getRecords().iterator();
    }
}

